/*
 * Copyright (C) 2010 Raimundo Mirisola (raimundo.mirisola@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.onerm.ui;

import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.google.common.collect.Maps;
import com.onerm.store.DataInterface;
import com.onerm.store.DataInterfaceFactory;

public class OneRM extends Activity {

  private Spinner weights;
  private Spinner reps;
  private TextView oneRmResult;
  private RMTable rmTable;

  private final Map<Integer, Dialog> dialogs = Maps.newHashMap();

  private static final int ONE_RM_TABLE_DIALOG = 1;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DataInterface db = DataInterfaceFactory.getSqlInterface(this);

    LinearLayout mainPanel = new LinearLayout(this);
    mainPanel.setOrientation(LinearLayout.VERTICAL);
    weights = new Spinner(this);
    weights.setAdapter(new WeightListAdapter());
    weights.setPrompt("Weight");
    reps = new Spinner(this);
    reps.setPrompt("Number of reps");
    reps.setAdapter(new RepListAdapter());
    reps.setOnItemSelectedListener(new SelectionListener());
    weights.setOnItemSelectedListener(new SelectionListener());

    Spinner exercises = new Spinner(this);
    exercises.setPrompt("Exercises");
    exercises.setAdapter(new ExercisesAdapter(db));

    mainPanel.addView(PanelBuilder.newLinearPanel(this, PanelBuilder.newTextView(this, "Reps:   "),
        reps));
    mainPanel.addView(PanelBuilder.newLinearPanel(this, PanelBuilder.newTextView(this, "Weight: "),
        weights));
    mainPanel.addView(PanelBuilder.newLinearPanel(this, PanelBuilder.newTextView(this, "Exercise: "),
        exercises));
    oneRmResult = new TextView(this);
    oneRmResult.setText("Select # of reps and weight.");
    oneRmResult.setTextSize(30);
    mainPanel.addView(oneRmResult);
    rmTable = new RMTable(this);
    Button button = new Button(this);
    Dialog oneRmTableDialog = new Dialog(this);
    oneRmTableDialog.setContentView(rmTable);
    oneRmTableDialog.setTitle("Estimated performance");
    dialogs.put(ONE_RM_TABLE_DIALOG, oneRmTableDialog);
    oneRmResult.setOnClickListener(
    new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        OneRM.this.showDialog(ONE_RM_TABLE_DIALOG);
      }
    });

    setContentView(mainPanel);
  }

  private void updateOneRM() {
    final double weight = (Double) weights.getSelectedItem();
    final double numReps = (Integer) reps.getSelectedItem();
    final double oneRM;
    if (numReps <= 36) {
      oneRM = (36.0 / (37.0 - numReps)) * weight;
    } else {
      oneRM = (numReps / 30.0 + 1) * weight;
    }
    String textValue = String.format("1RM: %.2f", oneRM);
    oneRmResult.setText(textValue);
    rmTable.updateOneRM(oneRM);
  }

  private class SelectionListener implements OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
      updateOneRM();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
      // do nothing.
    }
  }

  @Override
  protected Dialog onCreateDialog(int id) {
    return dialogs.get(id);
  }
}
